<?

include '../includes/specs_functions.php';
include "../../includes/feed_class.php";
#------------------------------------------------------------------------
# Connect to PADS
$DB = getDB();
#-------------------------------$Session information
$SESSION = get_session($DB);
$CICUID = $SESSION->Character;
$THEME = $_POST["THEME"];
$CLEANNAME = $SESSION->GetCharacter('cleanname');
$NAME = $SESSION->GetCharacter('name');
$AVATAR = $SESSION->GetCharacter('avatar');
#---------------------------------------------------

$output = json_encode(array("status"=>"error", "message"=>"nok"));

if(isset($_POST['val']) && $CICUID != "" && isset($_POST['specId']))
{

	$sid = $_POST['specId'];
	$val = $_POST['val'];

	$abfrage = "INSERT INTO ".SPEC_TBL_RATINGS." (
						".SPEC_TBL_RATINGS_SPEC_ID."
						,".SPEC_TBL_RATINGS_USER_ID."
						,".SPEC_TBL_RATINGS_VALUE."
						,".SPEC_TBL_RATINGS_CDATE."
						,".SPEC_TBL_RATINGS_UDATE."
						 )
					VALUES (
						\"".$sid."\"
						,\"".$CICUID."\"
						,\"".$val."\"
						,\"".date('Y-m-d H:i:s')."\"
						,\"".date('Y-m-d H:i:s')."\"
						)
					ON DUPLICATE KEY
					UPDATE ".SPEC_TBL_RATINGS_VALUE." = \"".$val."\"";
					
	$result = $DB->RawQuery($abfrage);

	
	if ($result[1][1] == 0)
	{
	
		$table = "(
		".SPEC_TBL_SPECS." as spec 
		LEFT JOIN 
		".TBL_CHARACTERS." as user
		ON user.".TBL_CHARACTERS_CICUID." = spec.".SPEC_TBL_SPECS_USER_ID.")";
		$col = array("spec.".SPEC_TBL_SPECS_ID." as specId"
			   ,"spec.".SPEC_TBL_SPECS_USER_ID." as userId"
			   ,"spec.".SPEC_TBL_SPECS_LAYOUT." as layout"
			   ,"spec.".SPEC_TBL_SPECS_JOB_ID." as jobId"
			   ,"spec.".SPEC_TBL_SPECS_LEVEL." as level"
			   ,"spec.".SPEC_TBL_SPECS_DESCRIPTION." as description"
			   ,"spec.".SPEC_TBL_SPECS_PUBLIC." as public"
			   ,"spec.".SPEC_TBL_SPECS_CDATE." as cdate"
			   ,"DATE_FORMAT(spec.".SPEC_TBL_SPECS_UDATE.",'%b %d %Y') as udate"
			   ,"user.".TBL_CHARACTERS_NAME." as Name"
			   );
		$where = array("spec.".SPEC_TBL_SPECS_ID." = \"".$sid."\"");
		$orderBy = "spec.".SPEC_TBL_SPECS_ID;
		$direction = "ASC";

		$result = $DB->GetData($table ,$col ,true ,$where, $orderBy, $direction);
		
		
		foreach ($result[3] as $row) {
		
			
			//TBL_MESSAGE_TO 			."='". $row['userId'] ."'",	
			
			$DB->Insert(TBL_MESSAGE,
				array(TBL_MESSAGE_TO 			."='".$row['userId']."'",	
					TBL_MESSAGE_FROM 		."='". $CICUID ."'",
					TBL_MESSAGE_ID 			."='". Random32() ."'",
					TBL_MESSAGE_TIME 		."='". time() ."'",
					TBL_MESSAGE_IPLONG 		."='". ip2long($_SERVER['REMOTE_ADDR']) ."'",
					TBL_MESSAGE_SUBJECT 	."='Specs: New Rating!'",
					TBL_MESSAGE_MESSAGE 	."=\"Dear ".$row['Name']."<br><strong>".$SESSION->GetCharacter('name')."</strong> rated your <strong>".GetClass($row['jobId'])."</strong> spec with a <strong>".$val."</strong>/5!<br><br>XIVPads\"",
					TBL_MESSAGE_STATUS 		."='1'",
					TBL_MESSAGE_RESOLVED 	."='0'",
					TBL_MESSAGE_TYPE 		."='s_rating'"));
					
			$DB->Insert(TBL_PAD_SHOUT,
				array(TBL_PAD_SHOUT_PAD 		."='".$row['userId']."'",	
					TBL_PAD_SHOUT_CICUID 		."='". $CICUID ."'",
					TBL_PAD_SHOUT_CHARACTER 	."='". $CLEANNAME ."'",
					TBL_PAD_SHOUT_USER 			."='". time() ."'",
					TBL_PAD_SHOUT_IPLONG 		."='". ip2long($_SERVER['REMOTE_ADDR']) ."'",
					TBL_PAD_SHOUT_MESSAGES 		."=\" [color=#000000][size=10px][b]Rated [color=#4380F9]".$row['Name']."[/color] 's [color=#4380F9]".GetClass($row['jobId'])."[/color] spec with a [color=#4380F9]".$val."[/color]/5![/b][/size][/color] \"",
					TBL_PAD_SHOUT_DATE 			."='". time() ."'",
					TBL_PAD_SHOUT_NOTIFICATION ."='1'")
					);
		}
		
		$Feed = new Feed($DB);
		$Message = '<strong><a href="?'. $CICUID .'">'. $NAME .'</a></strong> Rated <a href="?'. $row['userId'] .'">'. $row['Name'] .'</a></strong> \'s spec.';
		$Feed->Add($CICUID, $NAME, $AVATAR, 's_rating', $Message);
					
		$output = json_encode(array("status"=>"success", "message"=>"ok"));
	}else{
		$output = json_encode(array("status"=>"error", "message"=>$result[0][1]));
	}

}
echo $output;

#------------------------------------------------------------------------
# Disconnect
leaveDB($DB);

?>